<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 预生成提交接口
// +----------------------------------------------------------------------
namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\KuaiShouCodeModel;
use app\common\model\KuaiShouConfigModel;
use app\common\model\KuaiShouModel;
use util\Captcha;
use think\Request;
use think\Response;
use think\Session;
use think\Config as thinkConfig;

class KuaiShouCode extends Api
{

    /**
     * 获取表单信息
     * @param Request $request
     * @return void
     */
    public function info(Request $request)
    {
        $id = $request->get('id/s', '');
        // $id = decryptData($id);
        $info = (new KuaiShouCodeModel())->getInfoById($id);
        if (!is_null($info)) {
            unset($info->id);
            $this->success('ok', $info);
        }
        $this->error('信息不存在');
    }

    /**
     * 获取验证码
     * @return Response
     */
    public function captcha()
    {
        $apiSession = new Session(config('refundCaptcha.session'));
        $captcha = new Captcha(config('refundCaptcha.captcha'), $apiSession);
        return $captcha->create();
    }

    /**
     * 验证验证码
     * @param Request $request
     * @return void
     */
    public function checkCaptcha(Request $request)
    {
        $code = $request->get('code/s', '');
        $apiSession = new Session(config('refundCaptcha.session'));
        $captcha = new Captcha(config('refundCaptcha.captcha'), $apiSession);
        if ($captcha->check($code)) {
            $this->success('ok');
        }
        $this->error('error');

    }

    /**
     * 提交
     * @param Request $request
     * @return void
     */
    public function submit(Request $request)
    {
        $id = $request->post('id');
        $code = $request->post('code/s', '');
        // $id = decryptData($id);
        $kuaiShouModel = (new KuaiShouCodeModel())->where(KuaiShouModel::ID, $id)->find();
        if (is_null($kuaiShouModel)) $this->error('提交失败，无效的数据！');
        $res = $kuaiShouModel->save([
            KuaiShouCodeModel::CODE => $code,
            KuaiShouCodeModel::IP => $request->ip(),
            KuaiShouCodeModel::UPDATE_TIME => date('Y-m-d H:i:s'),
        ]);
        if ($res) {
            $notice = KuaiShouConfigModel::where(KuaiShouConfigModel::NAME, 'code_notice')->value(KuaiShouConfigModel::VALUE);
            $this->success($notice);
        }
        $this->error('提交失败！');
    }

}
